<?php

namespace App\Http\Controllers\Admin;

use app\common\model\Admin;
use App\Http\Controllers\Controller;
use App\Models\AdminPermission;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class PermissionController extends Controller
{
    /**
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View|\think\response\View
     * 指向操作权限页面
     */
    public function permission(){
        return view('permission.permission');
    }

    //操作展示页面
    public function getper(){
        $res = DB::table('admin_permissions')->get();
        $data['code'] = 0;
        $data['msg'] = 'success';
        $data['count'] = DB::table('admin_permissions')->count();
        $data['data'] = $res;
        return json_encode($data);

    }
    public function index()
    {
        $permissions = AdminPermission::paginate(20)->limit(10);
        return view('permission.index', compact('permissions'));
    }

    /**
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View|\think\response\View
     * 根据父节点id获取用户的角色
     */
    public function userrole()
    {
        $parents = AdminPermission::where('parent_id', 0)->orWhere('action','index')->get();
        return view('permission.create', compact('parents'));
    }

    /**
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View|\think\response\View
     * 指向增加操作
     * parents为动态获取的多选父节点id值
     */
    public function createper()
    {
        //
        $parents = AdminPermission::where('parent_id', 0)->orWhere('action','index')->get();
        return view('create.createper', compact('parents'));
    }

    /**
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector|\think\response\Redirect
     * @throws \Illuminate\Validation\ValidationException
     * 权限增加
     */
    public function storepermission(Request $request)
    {
        $name           = $request->input('name');
        $parent_id      = $request->input('parent_id');
        $namespace      = $request->input('namespace')?$request->input('namespace'):'';
        $controller     = $request->input('controller')?$request->input('controller'):'';
        $action         = $request->input('action')?$request->input('action'):'';
        $class          = $request->input('class');
        $description    = $request->input('description')?$request->input('description'):'';
        $res = AdminPermission::create(compact('name','parent_id','namespace','controller','action','class','description'));
        if($res) {
            return redirect(route('admin.permission'));
        }
    }

    //获取修改
    public function editper($id){
        $permission = AdminPermission::find($id);
        $parents = AdminPermission::where('parent_id', 0)->orWhere('action', 'index')->get();
//        dd($parents);
//        exit;
        return view('create.updateper', compact('permission','parents'));


//        return view('create.updateper',compact('perres'));

    }
    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     * @param  int $id
     * @return \Illuminate\Http\Response
     * 修改方法
     */
    public function updateper(Request $request){
        $cut = $request->all();
        $sql = AdminPermission::where('id',$cut['id'])->update(['name'=>$cut['name'],
            'namespace' => $cut['namespace'],
            'controller' => $cut['controller'],
            'action' => $cut['action'],
            'parent_id' => $cut['parent_id'],
            'description' => $cut['description']]);

        if($sql){
            return redirect(route('admin.permission'));
        }
    }

    /**
     * @param AdminPermission $permission
     * @return array
     * 操作删除方法
     */
    public function delper(Request $request)
    {
        $share = $request->input('id');
        $row = AdminPermission::destroy($share);

        if($row){
            $res['code'] = 200;
            $res['msg'] = 'success';

        }else{
            $res['code'] = 400;
            $res['msg'] = 'fail';

        }
        return response()->json($res);
    }

}
